package com.ruoyi.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.data.utils.MD5Utils;
import com.ruoyi.data.utils.OssUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.ruoyi.common.core.web.domain.AjaxResult.error;
import static com.ruoyi.common.core.web.domain.AjaxResult.success;

/**
 * @Author: 少康
 * @Date: 2023/04/25/16:59
 */
@RestController
@RequestMapping("/check")
public class CheckController {
//    @Autowired
//    private OSS oss;
    private static final String appId = "gSPhmyV8uvf4ogQ6";
    private static final String appSecurity = "gSPhmyV8uvf4ogQ6i86esvW0YeGRjORP";

    OssUtils ossUtils=new OssUtils();
    @PostMapping("/upload")
    public void upload(MultipartFile file) throws IOException {
//        String filePath = file.getOriginalFilename();
//        String suffix = filePath.substring(filePath.lastIndexOf(".") + 1);
//        String fileName="shenfz"+"."+suffix;
        String path = ossUtils.uploadOneFile(file);
        System.out.println(path);
    }

    //身份证正反面验证  已完成
    @PostMapping("/nationalUpload")
    public Map nationalUpload(MultipartFile file) throws IOException {
        String path = ossUtils.uploadOneFile(file);

        String appId="gSPhmyV8uvf4ogQ6";
        String appSecurity="gSPhmyV8uvf4ogQ6i86esvW0YeGRjORP";
        String timestamp = System.currentTimeMillis()+"";

        String sign = DigestUtils.md5Hex(appId + "&" + timestamp + "&" + appSecurity);
        System.err.println("sign: "+sign);

        InputStream inputStream = file.getInputStream();
        byte[] data=new byte[inputStream.available()];
        inputStream.read(data);
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Str = encoder.encode(data);

        String ocrUrl = "https://api.shumaidata.com/v2/idcard/ocr";

        //httpClient   restTemplate
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(ocrUrl);
        ArrayList<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("appid",appId));
        list.add(new BasicNameValuePair("timestamp",timestamp));
        list.add(new BasicNameValuePair("sign",sign));
        list.add(new BasicNameValuePair("image",base64Str));

        Map info = null;
        HttpEntity entity = new UrlEncodedFormEntity(list);
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost);
        String json = EntityUtils.toString(response.getEntity(), "UTF-8");
        Map map = JSONObject.parseObject(json, Map.class);
        if((Boolean) map.get("success")==true && (Integer)map.get("code")==200){
            Map res = (Map)map.get("data");
            info = (Map)res.get("info");
            info.put("path", path);
            inputStream.close();
            return success(info);
        }else {
            inputStream.close();
            return error("身份证正面验证未通过!");
        }
    }

    //OCR营业执照  已完成
    @PostMapping("/OcrYingYe")
    public String OcrYingYe(MultipartFile file) throws IOException {
        String path = ossUtils.uploadOneFile(file);
        String appId="gSPhmyV8uvf4ogQ6";
        String appSecurity="gSPhmyV8uvf4ogQ6i86esvW0YeGRjORP";
        String timestamp = System.currentTimeMillis() + "";

        //接口地址
        String url = "https://api.shumaidata.com/v2/business_license/ocr";

        //请下载MD5Utils文件,地址为 https://file.tianyandata.cn/demo/utils/MD5Utils.java
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        InputStream inputStream = file.getInputStream();
        byte[] data=new byte[inputStream.available()];
        inputStream.read(data);
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Str = encoder.encode(data);

        Map<String,String> headers = new HashMap<String,String>();
        headers.put("Content-Type","application/x-www-form-urlencoded");
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("image", base64Str);
        //请下载 HttpUtils文件，地址为 https://file.tianyandata.cn/demo/utils/HttpUtils.java
        String result = com.shumai.utils.HttpUtils.postForm(url,headers,params);
        System.out.println(result);
        inputStream.close();
        return result;
    }

    //活体检测  有错误
    @PostMapping("/HuoTi")
    public void huoti(MultipartFile file) throws IOException {
        String path = ossUtils.uploadOneFile(file);
        String appId="gSPhmyV8uvf4ogQ6";
        String appSecurity="gSPhmyV8uvf4ogQ6i86esvW0YeGRjORP";

        String timestamp = System.currentTimeMillis() + "";
        String motions = "YAW";
        String complexity = "2";
        //接口地址
        String url = "https://api.shumaidata.com/v2/life/check";

        //请下载MD5Utils文件,地址为 https://file.tianyandata.cn/demo/utils/MD5Utils.java
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        InputStream inputStream = file.getInputStream();
        byte[] data=new byte[inputStream.available()];
        inputStream.read(data);
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Str = encoder.encode(data);

        Map<String,String> headers = new HashMap<String,String>();
        headers.put("Content-Type","application/x-www-form-urlencoded");
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("image", base64Str);
        //请下载 HttpUtils文件，地址为 https://file.tianyandata.cn/demo/utils/HttpUtils.java
        String result = com.shumai.utils.HttpUtils.postForm(url,headers,params);
        System.out.println(result);
        inputStream.close();
    }

    //企业四要素认证
    @GetMapping("/QiYe")
    public void qiye(){
        String appId="gSPhmyV8uvf4ogQ6";
        String appSecurity="gSPhmyV8uvf4ogQ6i86esvW0YeGRjORP";

        String timestamp = System.currentTimeMillis() + "";
        //参数
        String companyName = "北京卢云科技有限公司";
        String creditNo = "91110115MA02XF259";
        String legalPerson = "张少康";
        String idCard = "110104199109062510";

        //接口地址
        String url = "https://api.shumaidata.com/v4/company-four/check";
        //请下载MD5Utils文件,地址为 https://file.tianyandata.cn/demo/utils/MD5Utils.java
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("companyName",companyName);
        params.put("creditNo",creditNo);
        params.put("legalPerson",legalPerson);
        params.put("idCard",idCard);

        //请下载 HttpUtils文件，地址为 https://file.tianyandata.cn/demo/utils/HttpUtils.java
        String result = com.shumai.utils.HttpUtils.get(url,null,params);
        System.out.println(result);
    }
}
